/**
 * 除法保留两位小数
 *
 * @param {*} divisor 除数
 * @param {*} dividend 被除数
 * @param {*} decimal 保留小数位数，默认为2
 * @returns
 */
export const divide = (divisor: number, dividend: number, decimal: number = 2) => {
	/**
  if (divisor == null || dividend == null || dividend === 0) {
    return null
  }
  return Math.floor((divisor / dividend) * 100) / 100
   */
	if (divisor == undefined || divisor == null) return 0.0
	return (divisor / dividend).toFixed(decimal)
}

/**
 * 计算时间
 * @param {number} seconds 秒数
 * @returns 数组 第一个元素为单位，第二个元素为数值
 */
export const convertSecondsToUnit = seconds => {
	if (typeof seconds !== 'number' || seconds < 0) {
		return []
	}

	const timeUnits = [
		{ unit: 'year', seconds: 365 * 24 * 60 * 60 },
		{ unit: 'month', seconds: 30 * 24 * 60 * 60 },
		{ unit: 'day', seconds: 24 * 60 * 60 },
	]

	for (const { unit, seconds: unitSeconds } of timeUnits) {
		const result = seconds / unitSeconds
		if (Number.isInteger(result)) {
			return [unit, result]
		}
	}

	return []
}
